package com.xlisteven.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xlisteven.entity.Fruits;
import com.xlisteven.entity.Product;
import com.xlisteven.entity.User;
import com.xlisteven.feign.FruitsClient;
import com.xlisteven.feign.ProductClient;
import com.xlisteven.mapper.UserMapper;
import com.xlisteven.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户服务impl
 *
 * @author xlisteven
 * @date 2022/09/29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private ProductClient productClient;
    @Resource
    private FruitsClient fruitsClient;

    @Override
    public User getOne(Long id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public void insertData(User user) {
        List<Product> productList = user.getProductList();
        List<Fruits> fruitsList = user.getFruitsList();
        //新增用户表
        userMapper.insert(user);
        Long userId = user.getId();
        //再新增商品表
        productList.forEach(product -> {
            product.setUserId(userId);
        });
        productClient.insert(productList);
        int a = 1 / 0;
        //再新增水果表
        fruitsList.forEach(fruits -> {
            fruits.setUserId(userId);
        });
        fruitsClient.insert(fruitsList);

    }
}
